package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService{
	@Autowired
	private CustomerTransferMapper customerTransferMapper;

	@Autowired
	private CustomerMapper customerMapper;
	@Override
	public void insert(CustomerTransfer customerTransfer) {
		//先往这张表 客户移交表插入数据
		customerTransfer.setTransUser((Employee) SecurityUtils.getSubject().getPrincipal());
		customerTransfer.setTransTime(new Date());
		customerTransferMapper.insert(customerTransfer);

		customerMapper.updateSeller(customerTransfer.getCustomer().getId(),customerTransfer.getNewSeller().getId());
	}


	@Override
	public CustomerTransfer select(Long id) {
		CustomerTransfer customerTransfer = customerTransferMapper.select(id);
		return customerTransfer;
	}

	@Override
	public List<CustomerTransfer> list() {
		List<CustomerTransfer> list = customerTransferMapper.list();
		return list;
	}

	public PageInfo<CustomerTransfer> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CustomerTransfer> customerTransfers = customerTransferMapper.selectForList(qo);
        return new PageInfo<>(customerTransfers);
	}



}
